/*
 * AjaxResponse.java
 * Copyright 2021 Qunhe Tech, all rights reserved.
 * Qunhe PROPRIETARY/CONFIDENTIAL, any form of usage is subject to approval.
 */

package top.bbman.boot.repository.vo;



import top.bbman.boot.repository.base.PeaErrorCode;
import top.bbman.boot.repository.enums.PeaCodeEnum;

import java.io.Serializable;

/**
 * ajax response
 *
 * @param <T>
 * @author 祥霸
 * @version 1.0.0
 */
public class PeaOut<T> implements Serializable {

    private static final long serialVersionUID = -1L;

    private int code;

    private String desc;

    private T data;

    public PeaOut(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

	public static PeaOut<String> success() {
		return PeaOut.buildResponse(PeaCodeEnum.SUCCESS.code(), "", null);
	}

	public static <T> PeaOut<T> success(T data) {
		return PeaOut.buildResponse(PeaCodeEnum.SUCCESS.code(), "", data);
	}

    public static <T> PeaOut<T> build(PeaErrorCode errorCode) {
        return new PeaOut<>(errorCode.code(), errorCode.msg());
    }

    public static <T> PeaOut<T> build(int code, String msg) {
        return new PeaOut<>(code, msg);
    }

    public static <T> PeaOut<T> build(PeaErrorCode errorCode, T data) {
        return new PeaOut<>(errorCode.code(), errorCode.msg(), data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return PeaCodeEnum.SUCCESS.code() == this.code;
    }

    public static <T> PeaOut<T> buildResponse(int code, String msg, T data) {
        return new PeaOut<>(code, msg, data);
    }

    /**
     * 构造器
     */
    public PeaOut() {
    }
    public PeaOut(int code) {
        this.code = code;
    }
    public PeaOut(final T data) {
        this.code = 200;
        this.data = data;
    }
    public PeaOut(int code, String desc, T data) {
        this.code = code;
        this.desc = desc;
        this.data = data;
    }

}
